package cn.smb.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import cn.smb.dao.UserMapper;
import cn.smb.entity.Page;
import cn.smb.service.RoleService;
import cn.smb.service.UserService;
import cn.smb.util.MyBatisUtil;

public class MainServlet extends HttpServlet {

	private final String PREFIX = "/WEB-INF/jsp/";

	/**
	 * Constructor of the object.
	 */
	public MainServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	private Map<String, Method> methods = new HashMap<String, Method>();

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");// 解决不了get问题
		response.setContentType("text/html;charset=utf-8");

		String path = request.getServletPath();
		path = path.substring(1, path.length() - 3);
		// path就是方法名
		try {
			if (path.equals("service") || path.equals("doGet")
					|| path.equals("doPost")) {
				response.sendError(500, "禁止访问此地址");
				return;
			}
			Method m = null;
			synchronized (methods) {
				m = methods.get(path);//
				if (m == null) {
					m = this.getClass()
							.getDeclaredMethod(path, HttpServletRequest.class,
									HttpServletResponse.class);
					methods.put(path, m);
				}
			}

			m.invoke(this, request, response);
			// 一步秒杀
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			response.sendError(500, "地址[" + path + "]不存在！");
		}

	}

	public void users(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获得所有的角色！
		request.setAttribute("roles", RoleService.SERVICE.findAll());
		String username = request.getParameter("username");
		Long role = null;
		int pageNo = 1;
		try {
			role = Long.valueOf(request.getParameter("role"));
		} catch (Exception e) {

		}
		try {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		} catch (Exception e) {

		}
		request.setAttribute("username", username);
		request.setAttribute("role", role);
		request.setAttribute("p",
				UserService.SERVICE.findAll(username, role, pageNo));

		request.getRequestDispatcher(PREFIX + "users.jsp").forward(request,
				response);
	}

	public void userDelM(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String[] ids = request.getParameterValues("userid");
		Long uid[] = new Long[ids.length];
		for (int i = 0; i < ids.length; i++) {
			uid[i] = Long.parseLong(ids[i]);
		}
		int r = UserService.SERVICE.deleteM(uid);

		request.getSession().setAttribute("msg", "删除了" + r + "条记录");
		response.sendRedirect("users.do");
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
